//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------
/*
*/

package uxgenes.core;


//
// Native Library and Java Interface
//

public final class NativeLib
						// #ifdef LICENSE_MANAGER
							implements com.google.android.vending.licensing.LicenseCheckerCallback
						// #endif LICENSE_MANAGER
{
	private EngineContext	_engine;

	private static final String		TAG = "NativeLib";
	
	public NativeLib (EngineContext eng)
	{
		_engine = eng;
	}
	
	
// Native functions - called from Java //
	
	public native void OnInit (Object assetManager, int supportedSensors);
	public native void OnQuit ();

	public native void OnStart ();
	public native void OnStop ();

	public native void OnPause ();
	public native void OnResume ();
	
	public native void OnSurfaceChanged (Object surface, int width, int height);
	public native void OnSurfaceDestroyed ();
	
	public native void VisibilityChanged (boolean isVisible);
	public native void AudioFocusChanged (int state);
	
	public native void SetDirectory (String internal, String internalCache,
			String external, String externalCache, String externalStorage);
	
	public native int  ProcessMessages ();	// returns delay for next call
	public native void LowMemory ();
	  
	public native void SetDeviceInfo (String name, String os, String deviceID,
									  String packageName, String locale,
									  int numCPUs, int cpuFreq, long totalMem, int apiVersion,
									  int width, int height,
									  int ppi, int orientation);
	
	public native void OnDisplayOrientationChanged (int orientation);
	
	public static native void MessageResult (int id, int result);	// > 0 - positive, < 0 - negative
	
	public native void WriteToLog (String msg, String file, int line);
	
	// input
	public native void OnTouch (int action, int count, float[] data);	// data - array of struct { float id, x, y, pressure; } [count]
																					// action - bitfield
	public native void OnKey (int keycode, boolean down);
	public native void OnAcceleration (float x, float y, float z);
	public native void OnMagneticField (float x, float y, float z);
	public native void OnGyroscope (float x, float y, float z);
	public native void OnOrientation (float x, float y, float z);
	public native void OnRotation (float x, float y, float z);
	public native void OnLightSensor (float level);
	public native void OnProximitySensor (float distance);
	public native void OnLocation (long time, double lon, double lat, float alt, float accur, float speed);
	public native void SetLocationStatus (int state);
	
	// camera callbacks
	public native void CameraPreview (byte[] data, int width, int height, int format);
	public native void CameraPicture (byte[] data, int width, int height, int format);
	public native void CameraPreviewAvailable (Object surfaceTexture);
	
	public native void CameraZoomChanged (int currentZoom, int maxZoom,
										  float nearFocus, float optimalFocus, float farFocus,
										  float focalLength, float vertViewAngle, float horViewAngle);
	
	public native void CameraFaceDetected (int id, int rectLeft, int rectBottom, int rectRight, int rectTop,
											int leftEyeX, int leftEyeY, int rightEyeX, int rightEyeY,
											int mouthX, int mouthY, int score);
	// camera params
	public native void CameraSetStaticParams (int minExposure, int maxExposure, float exposureStep,
											  int[] pictureFormats, int[] pictureSizes,
											  int[] previewFormats, int[] previewSizes, int[] previewFpsRanges,
											  int[] antibandings, int[] colorEffects, int[] flashModes,
											  int[] focusModes, int[] sceneModes, int[] whiteBalances);
	
	// event
	public native void SendEngineEvent (int event, int param0, int param1, int param2);

	
// LicenseCheckerCallback //
	// #ifdef LICENSE_MANAGER
	public native void allow (int policyReason);
	public native void dontAllow (int policyReason);
	public native void applicationError (int errorCode);
	// #endif LICENSE_MANAGER
	    
	
// Java functions - called from native //
	public final void CloseActivity ()
	{
		Logger.I( TAG, "CloseActivity" );
		_engine.CloseEngine();
	}
	
	public final void Notification (String text, boolean longTime)
	{
		Logger.I( TAG, "Notification" );
		_engine.ShowNotification( text, longTime );
	}
	
	public final void Message (int id, String caption, String text, String positive, String negative)
	{
		Logger.I( TAG, "Message" );
		_engine.ShowMessage( id, caption, text, positive, negative );
	}
	
	public final void SetSensorState (int sensor, boolean enabled, int delay)
	{
		Logger.I( TAG, "SetSensorState" );
		_engine.SetSensorState( sensor, enabled, delay );
	}
	
	public final void SendEmail (String address, String subject, String text, String attachment)
	{
		Logger.I( TAG, "SendEmail" );
		_engine.SendEmail( address, subject, text, attachment );
	}
	/*
	public final void BackgroundMode (boolean enabled)
	{
		Logger.I( TAG, "BackgroundMode" );
		_engine.BackgroundMode( enabled );
	}
	*/
	public final void EnableAudioFocus (boolean enable)
	{
		Logger.I( TAG, "EnableAudioFocus" );
		if ( enable ) {
			_engine.RequestAudioFocus();
		} else {
			_engine.AbandonAudioFocus();
		}
	}
	
	public final void Vibrate (long[] pattern)
	{
		Logger.I( TAG, "Vibrate" );
		_engine.Vibrate( pattern );
	}
	
	public final void OpenURL (String url)
	{
		Logger.I( TAG, "OpenURL" );
		_engine.OpenURL( url );
	}
	
	public final void SetDisplayOrientation (int orient)
	{
		Logger.I( TAG, "SetDisplayOrientation" );
		_engine.SetDisplayOrientation( orient );
	}
	
	public final android.content.Context GetContext ()
	{
		return _engine.GetContext();
	}
	
	public final boolean IsNetworkConnected ()
	{
		return _engine.IsNetworkConnected();
	}
	
	
	// Extensions
	public final void EnableExtension (int ext, boolean enable)
	{
		Logger.I( TAG, "EnableExtension" );
		_engine.EnableExtension( ext, enable );
	}
	
	// Camera
	public final void CameraCommand (int cmd, float arg0, float arg1, float arg2)
	{
		if ( _engine.camera != null ) {
			Logger.I( TAG, "CameraCommand 0x" + Integer.toString( cmd, 16 ) );
			_engine.camera.CameraCommand( cmd, arg0, arg1, arg2 );
		}
	}
	
	// events
	public final void ProcessEngineEvent (int event, int param0, int param1, int param2)
	{
		Logger.I( TAG, "ProcessEngineEvent" );
		_engine.ProcessEngineEvent( event, param0, param1, param2 );
	}
}
